import plotly.express as px
import plotly as pl
pl.offline.init_notebook_mode()
df = px.data.gapminder().query("year==2007")
fig = px.scatter_geo(df, locations="iso_alpha", color="continent",
hover_name="country", size="pop",
projection="natural earth")
fig.show()
The above dataset is Gapminder, which consist of countries around the globe and giving insights of gdp, death rate, life expectancy and so on. This part of code display all the countries, with a pop up indicating "Continent,population and country code", using plotly express
gapminder = px.data.gapminder()
gapminder1987 = gapminder.query('year == 1987')
fig = px.choropleth(gapminder1987, locations='iso_alpha', color='lifeExp', hover_name='country',
animation_frame='year', color_continuous_scale=px.colors.sequential.Plasma, projection='natural earth')
fig.show()
The above code uses same plotly express but segregating countries based on year "1987", and "Life expectancy", to get better idea of rate of life Expectancy
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np
# Initialize figure with 4 3D subplots
fig = make_subplots(
rows=2, cols=2,
specs=[[{'type': 'surface'}, {'type': 'surface'}],
[{'type': 'surface'}, {'type': 'surface'}]])
# Generate data
x = np.linspace(-5, 80, 10)
y = np.linspace(-5, 60, 10)
xGrid, yGrid = np.meshgrid(y, x)
z = xGrid ** 3 + yGrid ** 3
# adding surfaces to subplots.
fig.add_trace(
go.Surface(x=x, y=y, z=z, colorscale='Viridis', showscale=False),
row=1, col=1)
fig.add_trace(
go.Surface(x=x, y=y, z=z, colorscale='RdBu', showscale=False),
row=1, col=2)
fig.add_trace(
go.Surface(x=x, y=y, z=z, colorscale='YlOrRd', showscale=False),
row=2, col=1)
fig.add_trace(
go.Surface(x=x, y=y, z=z, colorscale='YlGnBu', showscale=False),
row=2, col=2)
fig.update_layout(
title_text='3D subplots with different colorscales',
height=800,
width=800
)
fig.show()
I wrote this piece of code to get my hands on ploty as object.I found this package "Go", to make ploty graphs as object and based upon the function, I tweak around subplots.
import seaborn as sns
tips = sns.load_dataset("tips")
sns.catplot(data=tips, x="day", y="total_bill")
<seaborn.axisgrid.FacetGrid at 0x290c2d6a0>
I am a big fan of Catplot through out my whole visuallization journey, and it would be not fare not to include in this lab. The one fact of catplot is how easy and extensive its use is.
sns.catplot(
data=tips, x="total_bill", y="day", hue="sex",
kind="violin", bw=.15, cut=0,
)
<seaborn.axisgrid.FacetGrid at 0x290f32550>
This is also catplot but display probavility density, of realtion with 3 different features.